package com.ztc.ZtcCar.web;

import com.alibaba.fastjson.JSONObject;
import com.aqgj.Attachment.bean.Attachment;
import com.aqgj.Attachment.service.AttachmentManager;
import com.aqgj.User.bean.User;
import com.aqgj.util.JsonWrapper;
import com.aqgj.util.Pager;
import com.ztc.ZtcCar.bean.ZtcCar;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ztc.ZtcCar.service.ZtcCarManager;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("ztcCar")
public class ZtcCarAction {
    @Autowired
    private ZtcCarManager ztcCarManager;
    @Autowired
    private ZtcTransportManager ztcTransportManager;
    @Autowired
    private AttachmentManager attachmentManager;

    /**
     * 添加保存车信息.
     * @param ztcCar  车信息
     * @param imgString 车证照
     * @param request
     * @return
     */
    @RequestMapping("saveCar")
    @ResponseBody
    public HashMap<String, Object> saveCar(ZtcCar ztcCar,HttpSession session,String imgString,HttpServletRequest request,String pageStatus) {
        User user = (User) session.getAttribute("userInfo");
        ZtcTransport ztcTransport = ztcTransportManager.get(user.getTransportId());
        if(ztcTransport==null){
            return JsonWrapper.failureWrapperMsg("操作失败，请稍后重试！");
        }
        //当前公司状态处于冻结状态，无法操作业务
        if (ztcTransport.getStatus()==4) {
            return JsonWrapper.failureWrapperMsg("公司处于冻结状态，该操作不被允许!");
        }
        if(ztcTransport.getType()!=3&&ztcTransport.getType()!=4){
            return JsonWrapper.failureWrapperMsg("非运营或车方无权限进行该操作！");
        }
        if("add".equals(pageStatus)){
            if(ztcTransport.getType()==4){
                ztcCar.setStatus(2);
            }else{
                ztcCar.setStatus(1);
            }
            ztcCar.setTransportId(ztcTransport.getId());
            ztcCar.setType(1);//直通车
            String carId= ztcCarManager.save(ztcCar);
            JSONObject json = JSONObject.parseObject(imgString);
            if(json.size()<4){
                ztcCarManager.removeById(carId);
                return JsonWrapper.failureWrapperMsg("证件资料不全，无法提交数据！");
            }
            if (json.containsKey("carSide_img")) {
                attachmentManager.saveAttachment(json,"carSide_img",user,carId,request,"添加车辆");
            }
            if (json.containsKey("driver_lic_img")) {
                attachmentManager.saveAttachment(json,"driver_lic_img",user,carId,request,"添加车辆");
            }
            if (json.containsKey("Operation_img")) {
                attachmentManager.saveAttachment(json,"Operation_img",user,carId,request,"添加车辆");
            }
            if (json.containsKey("ride_img")) {
                attachmentManager.saveAttachment(json,"ride_img",user,carId,request,"添加车辆");
            }
        }
        if("modify".equals(pageStatus)){//修改车辆
            ZtcCar ztcCar1=ztcCarManager.get(ztcCar.getId());
            List<Attachment> list=attachmentManager.getAttachmentByBusinessId(ztcCar.getId());
            ztcCar.setTransportId(ztcCar1.getTransportId());
            ztcCar.setExamineTime(ztcCar1.getExamineTime());
            ztcCar.setExamineRemark(ztcCar1.getExamineRemark());
            ztcCar.setExamineUserId(ztcCar1.getExamineUserId());
            ztcCar.setModifyRemark(ztcCar1.getModifyRemark());
            ztcCar.setType(ztcCar1.getType());
            ztcCar.setOrigin(ztcCar1.getOrigin());
            ztcCar.setPurpose(ztcCar1.getPurpose());
            JSONObject json = JSONObject.parseObject(imgString);
            if (ztcTransport.getType() == 4) {//车方
                if (json.size() <= 0) {//未修改图片
                    ztcCar.setStatus(ztcCar1.getStatus());
                }else{//修改图片，需审核
                    ztcCar.setStatus(2);
                }
            }else{
                ztcCar.setStatus(ztcCar1.getStatus());
            }
            String carId= ztcCarManager.update(ztcCar);
            if (!"".equals(json.get("carSide_img"))) {
                if(list.size()>0){
                    for (Attachment a:list) {
                        if("carSide_img".equals(a.getWorkItem())){
                            attachmentManager.deleteAttachment(a);
                        }
                    }
                }
                attachmentManager.saveAttachment(json,"carSide_img",user,carId,request,"修改车辆");
            }
            if (!"".equals(json.get("driver_lic_img"))) {
                if(list.size()>0){
                    for (Attachment a:list) {
                        if("driver_lic_img".equals(a.getWorkItem())){
                            attachmentManager.deleteAttachment(a);
                        }
                    }
                }
                attachmentManager.saveAttachment(json,"driver_lic_img",user,carId,request,"修改车辆");
            }
            if (!"".equals(json.get("Operation_img"))) {
                if(list.size()>0){
                    for (Attachment a:list) {
                        if("Operation_img".equals(a.getWorkItem())){
                            attachmentManager.deleteAttachment(a);
                        }
                    }
                }
                attachmentManager.saveAttachment(json,"Operation_img",user,carId,request,"修改车辆");
            }
            if (!"".equals(json.get("ride_img"))) {
                if(list.size()>0){
                    for (Attachment a:list) {
                        if("ride_img".equals(a.getWorkItem())){
                            attachmentManager.deleteAttachment(a);
                        }
                    }
                }
                attachmentManager.saveAttachment(json,"ride_img",user,carId,request,"修改车辆");
            }
            return JsonWrapper.successWrapper("修改成功！");
        }
        return JsonWrapper.successWrapper("添加成功！");
    }

    /**
     * 当前公司下carList(运营、车方).
     * @param session  session
     * @param pageSize  每页大小
     * @param pageNumber 页码
     * @return
     */
    @RequestMapping("getCars")
    @ResponseBody
    public HashMap<String, Object> getCars(HttpSession session,Integer pageSize,Integer pageNumber,String search) {
        User user = (User) session.getAttribute("userInfo");
        ZtcTransport ztcTransport = ztcTransportManager.get(user.getTransportId());
        List<ZtcCar> list = new ArrayList<>();
        //当前操作公司的车(或旗下车方已审核的车)
        Pager<ZtcCar> carList = ztcCarManager.getZtcCarByTransportId(user.getTransportId(),pageSize,pageNumber,ztcTransport.getType(),search);
        list.addAll(carList.getContent());
        HashMap<String, Object> map = null;
        List<HashMap<String, Object>> list1 = new ArrayList<>();
        if (list.size() > 0) {
            for (ZtcCar car : list) {
                map = new HashMap<>();
                map.put("id", car.getId());
                map.put("status", getCarStatus(car.getStatus()));
                map.put("carType", getCarType(car.getCarType()));
                map.put("carBrand", car.getBrand());
                map.put("carNumber", car.getCarNumber());
                map.put("carColor", car.getColor());
                map.put("carSeat", car.getSeatNum());
                map.put("carProperty", getCarProperty(car.getProperty()));
                map.put("transport", ztcTransportManager.get(car.getTransportId()).getFullName());
                map.put("cStatus", car.getStatus());
                list1.add(map);
            }
            return JsonWrapper.successPagerWrapper(list1,carList.getRecordTotal());
        }
        return JsonWrapper.successWrapper("");
    }

    /**
     * 当前公司下carList(运营、车方).
     * @param session  session
     * @return
     */
    @RequestMapping("getAllCars")
    @ResponseBody
    public HashMap<String, Object> getAllCars(HttpSession session) {
        User user = (User) session.getAttribute("userInfo");
        ZtcTransport ztcTransport = ztcTransportManager.get(user.getTransportId());
        //当前操作公司的车(或旗下车方已审核的车)
        List<ZtcCar> carList = ztcCarManager.getZtcCarByTransportId(user.getTransportId(),ztcTransport.getType());
        HashMap<String, Object> map = null;
        List<HashMap<String, Object>> list1 = new ArrayList<>();
        if (carList.size() > 0) {
            for (ZtcCar car : carList) {
                map = new HashMap<>();
                map.put("id", car.getId());
                map.put("carNumber", car.getCarNumber());
                map.put("carType", car.getCarType());
                list1.add(map);
            }
            return JsonWrapper.successWrapper(list1);
        }
        return JsonWrapper.successWrapper("");
    }

    /**
     * 运营公司获取旗下车方待审核车.
     *
     * @param session 当前session
     * @return
     */
    @RequestMapping("getWillReviewed")
    @ResponseBody
    public HashMap<String, Object> getWillReviewed(HttpSession session,Integer pageSize,Integer pageNumber,String search) {
        User user = (User) session.getAttribute("userInfo");
        ZtcTransport ztcTransport = ztcTransportManager.get(user.getTransportId());
        Pager<ZtcCar> pager= ztcCarManager.getZtcCarByIdAndReviewed(ztcTransport.getId(),false,pageSize,pageNumber,search,ztcTransport.getType());
        HashMap<String, Object> map = null;
            List<HashMap<String, Object>> list1 = new ArrayList<>();
            if (pager.getContent().size() > 0) {
                for (ZtcCar car : pager.getContent()) {
                    map = new HashMap<>();
                    map.put("id", car.getId());
                    map.put("status", getCarStatus(car.getStatus()));
                    map.put("carType", getCarType(car.getCarType()));
                    map.put("carBrand", car.getBrand());
                    map.put("carNumber", car.getCarNumber());
                    map.put("carColor", car.getColor());
                    map.put("carSeat", car.getSeatNum());
                    map.put("cStatus", car.getStatus());
                    map.put("carProperty", getCarProperty(car.getProperty()));
                    map.put("transport", ztcTransportManager.get(car.getTransportId()).getFullName());
                    list1.add(map);
            }
            return JsonWrapper.successPagerWrapper(list1,pager.getRecordTotal());
        }
        return JsonWrapper.successWrapper("");
    }

    /**
     * 根据车id获取详情.
     *
     * @param cid 车id
     * @return
     */
    @RequestMapping("getCarDetail")
    @ResponseBody
    public HashMap<String, Object> getCarDetail(String cid) {
        ZtcCar ztcCar = ztcCarManager.get(cid);
        if (ztcCar == null) {
            return JsonWrapper.failureWrapperMsg("不存在该车信息！");
        }
        HashMap<String,Object> map=new HashMap<>();
        map.put("carType", getCarType(ztcCar.getCarType()));
        map.put("carBrand", ztcCar.getBrand());
        map.put("carNumber", ztcCar.getCarNumber());
        map.put("carColor", ztcCar.getColor());
        map.put("carSeat", ztcCar.getSeatNum());
        map.put("cStatus", ztcCar.getStatus());
        map.put("cid", ztcCar.getId());
        map.put("status", getCarStatus(ztcCar.getStatus()));
        map.put("carProperty", getCarProperty(ztcCar.getProperty()));
        map.put("transport", ztcTransportManager.get(ztcCar.getTransportId()).getFullName());
        List<Attachment> attachments = attachmentManager.getAttachmentByBusinessId(cid);
        if (attachments.size() > 0) {
            for (Attachment a : attachments) {
                if ("ride_img".equals(a.getWorkItem())) {
                    map.put("rFilePath", a.getFilePath());
                    map.put("rFileName", a.getFileName());
                    continue;
                }
                if ("Operation_img".equals(a.getWorkItem())) {
                    map.put("oFilePath", a.getFilePath());
                    map.put("oFileName", a.getFileName());
                    continue;
                }
                if ("carSide_img".equals(a.getWorkItem())) {
                    map.put("cFilePath", a.getFilePath());
                    map.put("cFileName", a.getFileName());
                    continue;
                }
                if ("driver_lic_img".equals(a.getWorkItem())) {
                    map.put("dFilePath", a.getFilePath());
                    map.put("dFileName", a.getFileName());
                }
            }
        }
        return JsonWrapper.successWrapper(map);
    }

    /**
     * 根据车id审核该车.
     * @param cid      车id
     * @param status  审核状态
     * @param remark  审核意见
     * @return
     */
    @RequestMapping("reviewedCar")
    @ResponseBody
    public HashMap<String, Object> reviewedCar(String cid,Integer status,String remark,HttpSession session) {
        ZtcCar ztcCar = ztcCarManager.get(cid);
        User user = (User) session.getAttribute("userInfo");
        ZtcTransport ztcTransport=ztcTransportManager.get(user.getTransportId());
        if (ztcTransport==null) {
            return JsonWrapper.failureWrapperMsg("网络错误，请稍后重试!");
        }
        //当前公司状态处于冻结状态，无法操作业务
        if (ztcTransport.getStatus()==4) {
            return JsonWrapper.failureWrapperMsg("公司处于冻结状态，该操作不被允许!");
        }
        if(ztcTransport.getType()!=3){
            return JsonWrapper.failureWrapperMsg("非运营公司无权限进行该操作！");
        }
        if (status !=1&&status!=4) {
            return JsonWrapper.failureWrapperMsg("状态参数指向性错误！");
        }
        if (ztcCar == null) {
            return JsonWrapper.failureWrapperMsg("不存在该车信息！");
        }
        if (ztcCar.getStatus()!=2) {
            return JsonWrapper.failureWrapperMsg("该车信息已审核！");
        }
        ztcCar.setStatus(status);
        ztcCar.setExamineRemark(remark);
        ztcCar.setExamineTime(new Date());
        ztcCar.setExamineUserId(user.getId());
        ztcCarManager.save(ztcCar);
        return JsonWrapper.successWrapper("操作成功！");
    }


    /**
     *冻结/启用.
     * @param cid     车id
     * @param status  状态
     * @return
     */
    @RequestMapping("setCarStatus")
    @ResponseBody
    public HashMap<String, Object> setCarStatus(HttpSession session,String cid,Integer status) {
        User user = (User) session.getAttribute("userInfo");
        ZtcTransport ztcTransport1 = ztcTransportManager.get(user.getTransportId());
        if (ztcTransport1==null) {
            return JsonWrapper.failureWrapperMsg("网络错误，请稍后重试!");
        }
        //当前公司状态处于冻结状态，无法操作业务
        if (ztcTransport1.getStatus()==4) {
            return JsonWrapper.failureWrapperMsg("公司处于冻结状态，该操作不被允许!");
        }
        ZtcCar ztcCar = ztcCarManager.get(cid);
        if (ztcCar == null) {
            return JsonWrapper.failureWrapperMsg("不存在该车信息！");
        }
        ztcCar.setStatus(status);
        ztcCarManager.save(ztcCar);
        return JsonWrapper.successWrapper("操作成功！");
    }


    //车类型
    private String getCarType(Integer type) {
        String temp = "";
        switch (type) {
            case 1:temp = "轿车";break;
            case 2:temp = "商务";break;
            case 3:temp = "大巴";break;
        }
        return temp;
    }

    //车状态
    private String getCarStatus(Integer status) {
        String temp = "";
        switch (status) {
            case 1:temp = "正常";break;
            case 2:temp = "待审核";break;
            case 3:temp = "禁用";break;
        }
        return temp;
    }

    //车性质
    private String getCarProperty(Integer property) {
        String temp = "";
        switch (property) {
            case 1:temp = "运营";break;
            case 2:temp = "租赁";break;
        }
        return temp;
    }

}


